<?php

require_once 'CommonAction.php';

class ConnectAction extends CommonAction {
	
	private $_host;
	private $_db;
	private $_user;
	private $_pass;
	
	public function __construct() {
		parent::__construct();
		parent::appendObjectList(new ObjectList("dbParameters", array(), ObjectList::_PUBLIC));
	}
	
	public function __request(Parameters $param) {
		parent::__request($param);
		$this->_host	= $this->getParameter("_host")->getValue();
		$this->_db		= $this->getParameter("_db")->getValue();
		$this->_user	= $this->getParameter("_user")->getValue();
		$this->_pass	= $this->getParameter("_pass")->getValue();
	}
	
	public function __response() {
		if ( parent::getResponse() ) {
			return "selectTable";
		} else {
			return "connect";
		}
	}
	
	public function __execute() {
		
		$dbParameter = array ( 	"host" 	=> $this->_host,
								"db"	=> $this->_db,
								"user"	=> $this->_user,
								"pass"	=> $this->_pass );
		// Test database connection
		$db = new mysqli($dbParameter['host'], $dbParameter['user'], $dbParameter['pass'], $dbParameter['db']);
		if (mysqli_connect_errno()) {
			parent::appendError(new Error ( Error::_DB_CONNECT_ERROR, mysqli_connect_error(), Error::_ERROR));
		}
		
		// Checks only error messages
		if ( parent::countErrors(Error::_ERROR) == 0 ) {
			$_dbParameter = parent::getObjectList("dbParameter");
			$_dbParameter->setValue($dbParameter);
			parent::appendObjectList($_dbParameter);
			parent::setResponse(CommonAction::_SUCCESS);
		} else {
			parent::setResponse(CommonAction::_FAIL);
		}
	}
}

?>